package com.download.utils;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

public class ConvertUtil {

    private ConvertUtil(){
    }

    /**
     * 浮点数转为指定精确度的字符串
     * @param srcNum 要转换的数字
     * @param inimumFractionDigits 精确度
     * @return
     */
    public static String float2Percent(float srcNum, int inimumFractionDigits) {
        NumberFormat nt = NumberFormat.getPercentInstance();
        nt.setMinimumFractionDigits(inimumFractionDigits);
        return nt.format(srcNum);
    }

    /**
     * long数组转为字符串的方法
     * @param longArray
     * @return
     */
    public static String longArray2String(long[] longArray) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < longArray.length; i++) {
            sb.append(longArray[i]).append(",");
        }
        sb = sb.replace(sb.lastIndexOf(","), sb.length(),"]"); 
        return sb.toString();
    }

    /**
     * Long对象数组转换为long数组
     * @param list
     * @return
     */
    public static long[] convertLong2long(List<Long> list) {
        long[] longArray = new long[list.size()];  
        for (int i = 0; i < longArray.length; i++) {
            longArray[i] = list.get(i).longValue();
        }
        return longArray;
    }

    public static String formatFloat(float srcNum, String pattern) {
        DecimalFormat decimal = new DecimalFormat(pattern);  
        return  decimal.format(srcNum);  
    }
}
